
import type { App } from 'vue'
import type { I18n, I18nOptions } from 'vue-i18n'
import { createI18n } from 'vue-i18n'
import { locGet, locSet } from '@/utils/common'
import en from './en'
import zhCN from './zhCn'
export let i18n: ReturnType<typeof createI18n>

async function createI18nOptions (): Promise<I18nOptions> {
  if (!locGet('locale')) {
    locSet('locale', 'zhCN')
  }
  const locale = locGet('locale')
  const messages = {
    en,
    zhCN
  }
  return {
    fallbackLocale: 'zhCN',
    globalInjection: true,
    legacy: false, // you must specify 'legacy: false' option
    locale,
    messages
  }
}

export async function setupI18n (app: App) {
  const options = await createI18nOptions()
  const i18n = createI18n(options) as I18n
  app.use(i18n)
}
